package com.firesrc.mail.send;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;

import com.firesrc.bean.ExceptionLog;
import com.firesrc.bean.Record;
import com.firesrc.bean.SendRecord;
import com.firesrc.db.DBManager;
import com.firesrc.service.ExceptionLogService;
import com.firesrc.service.SendRecordService;
import com.firesrc.service.impl.ExceptionLogServiceImpl;
import com.firesrc.service.impl.SendRecordServiceImpl;
import com.firesrc.util.SystemUtil;

public class LogMailObserver implements MailObserver {
	private DbThreadSession dbThreadSession;
	private SendRecordService sendRecordService = new SendRecordServiceImpl();
	private ExceptionLogService exceptionLogService = new ExceptionLogServiceImpl();
	private MailSender mailSender;
	private static ConcurrentHashMap<String, Integer> failEmailMap = new ConcurrentHashMap<String, Integer>();

	public LogMailObserver(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	@Override
	public void success(Record record) {
		SendRecord sendRecord = new SendRecord();
		sendRecord.setId(UUID.randomUUID().toString());
		sendRecord.setFromMail(mailSender.getMail());
		sendRecord.setToMail(record.getMail());
		sendRecord.setKind(mailSender.getKind());
		sendRecord.setUserName(mailSender.getUserName());
		sendRecord.setSendDate(SystemUtil.getCurrentDateYYYYMMDD());
		sendRecord.setState("1");
		sendRecordService.add(sendRecord);

		dbThreadSession = new DbThreadSession(DBManager.getPublicConnectionPool());// fixme  要抽取出来到service里面
		DbOomQuery q1 = new DbOomQuery("update record set mailstate='1' ,sendmaildate='"
				+ SystemUtil.getCurrentDateStringYYYYMMDD() + "' where id='" + record.getId() + "'");
		q1.executeUpdate();
		dbThreadSession.closeSession();

	}

	@Override
	public void fail(Record record, Exception e) {
		Integer integer = failEmailMap.get(record.getMail());

		if (integer != null) {
			if (integer >= 2) {
				dbThreadSession = new DbThreadSession(DBManager.getPublicConnectionPool());// fixme  要抽取出来到service里面
				DbOomQuery q1 = new DbOomQuery("delete from record  where mail='" + record.getMail() + "'");
				q1.executeUpdate();
				dbThreadSession.closeSession();
				failEmailMap.remove(record.getMail());
				System.err.println("LogMailObserver.fail() 移除了2次不能发送的邮件地址:" + record.getMail());
				return;
			}
			failEmailMap.put(record.getMail(), integer + 1);
		} else {
			failEmailMap.put(record.getMail(), 1);
		}

		SendRecord sendRecord = new SendRecord();
		sendRecord.setId(UUID.randomUUID().toString());
		sendRecord.setFromMail(mailSender.getMail());
		sendRecord.setToMail(record.getMail());
		sendRecord.setKind(mailSender.getKind());
		sendRecord.setUserName(mailSender.getUserName());
		sendRecord.setSendDate(SystemUtil.getCurrentDateYYYYMMDD());
		sendRecord.setState("0");
		sendRecord.setErrorInfo(e.getCause().getLocalizedMessage());
		sendRecordService.add(sendRecord);
		System.out.println("LogMailObserver.fail()::::::::" + e.toString());

	}

	@Override
	public void handleOpenSessionFail(ExceptionLog e) {
		exceptionLogService.add(e);

	}

	@Override
	public void handleOpenSessionFail(Exception e) {
		exceptionLogService.add(e);

	}
}
